package com.atguigu.gmall.list.receiver;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.list.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author mqx
 * @date 2020-10-23 15:48:54
 */
@Component
public class ListReceiver {

    @Autowired
    private SearchService searchService;
    // 开启监听
    //  商品上架
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
      value = @Queue(value = MqConst.QUEUE_GOODS_UPPER),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.ROUTING_GOODS_UPPER}
    ))
    public void onSale(Long skuId, Message  message, Channel channel){
        //  这个里面做什么? 直接调用方法 upperGoods();
        if (skuId!=null){
            searchService.upperGoods(skuId);
        }

        //  手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //  商品下架
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_LOWER),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.ROUTING_GOODS_LOWER}
    ))
    public void lower(Long skuId, Message  message, Channel channel){
        //  这个里面做什么? 直接调用方法 upperGoods();
        if (skuId!=null){
            searchService.lowerGoods(skuId);
        }
        //  手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
